package com.doubao.notepad_blog_backend;

import com.doubao.notepad_blog_backend.jwt.JwtAuthenticationFilter;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.logging.Filter;

// 我们这里还没有配置数据库，exclude = {DataSourceAutoConfiguration.class就是启动时不加载数据库
// @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@Slf4j
@SpringBootApplication
@MapperScan("com.doubao.notepad_blog_backend.mapper")
@EnableSwagger2
public class NotepadBlogBackendApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder){
        return builder.sources( NotepadBlogBackendApplication.class );
    }

    // 注册过滤器
    @Bean
    public FilterRegistrationBean jwtFilter() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        JwtAuthenticationFilter filter = new JwtAuthenticationFilter();
        registrationBean.setFilter( filter );
        return registrationBean;
    }


    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run( NotepadBlogBackendApplication.class, args );
        //获取环境参数
        String port = context.getEnvironment().getProperty( "server.port" );
        log.info("http://localhost:"+port );
    }

}
